using CollabApp.Domain.ValueObjects;

namespace CollabApp.Application.DTOs.Game
{
    /// <summary>
    /// 画线圈地移动结果
    /// </summary>
    public class LineDrawingMoveResult
    {
        public bool Success { get; set; }
        public Position? NewPosition { get; set; }
        public double MovementSpeed { get; set; } = 1.0;
        public bool IsInEnemyTerritory { get; set; }
        public bool CanStartDrawing { get; set; } = true;
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 画线结果
    /// </summary>
    public class LineDrawingResult
    {
        public bool Success { get; set; }
        public bool IsDrawing { get; set; }
        public List<Position> CurrentPath { get; set; } = new();
        public double PathLength { get; set; }
        public bool PathTooLong { get; set; }
        public bool CollisionDetected { get; set; }
        public Guid? CollidedWithPlayerId { get; set; }
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 圈地完成结果
    /// </summary>
    public class TerritoryClaimResult
    {
        public bool Success { get; set; }
        public double NewTerritoryArea { get; set; }
        public double TotalTerritoryArea { get; set; }
        public List<Territory> EngulfedTerritories { get; set; } = new();
        public double EngulfedArea { get; set; }
        public int NewRank { get; set; }
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 碰撞检测结果
    /// </summary>
    public class CollisionCheckResult
    {
        public bool HasCollision { get; set; }
        public Guid? CollidedWithPlayerId { get; set; }
        public Position? CollisionPoint { get; set; }
        public string? CollisionType { get; set; } // "player_trail", "self_trail", "boundary"
        public bool ShouldDie { get; set; }
        public bool CanAvoid { get; set; } // 是否可以通过道具避免
    }

    /// <summary>
    /// 道具拾取结果
    /// </summary>
    public class PowerUpPickupResult
    {
        public bool Success { get; set; }
        public PowerUpType? PowerUpType { get; set; }
        public string? PowerUpName { get; set; }
        public string? Description { get; set; }
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 道具使用结果
    /// </summary>
    public class PowerUpUseResult
    {
        public bool Success { get; set; }
        public PowerUpType? UsedPowerUpType { get; set; }
        public string? Effect { get; set; }
        public int DurationMs { get; set; }
        public Dictionary<string, object>? EffectParameters { get; set; }
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 玩家死亡结果
    /// </summary>
    public class PlayerDeathResult
    {
        public bool Success { get; set; }
        public string? DeathReason { get; set; }
        public Guid? KillerPlayerId { get; set; }
        public double RemainingTerritoryArea { get; set; }
        public int RespawnTimeMs { get; set; }
        public bool HasDeathPenalty { get; set; }
        public Position SpawnPoint { get; set; } = new Position(0, 0);
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 玩家复活结果
    /// </summary>
    public class PlayerRespawnResult
    {
        public bool Success { get; set; }
        public Position SpawnPosition { get; set; } = new Position(0, 0);
        public bool IsInvincible { get; set; } = true;
        public int InvincibilityTimeMs { get; set; } = 5000;
        public List<string> Errors { get; set; } = new();
        public long Timestamp { get; set; }
    }

    /// <summary>
    /// 画线圈地玩家状态
    /// </summary>
    public class LineDrawingPlayerState
    {
        public Guid PlayerId { get; set; }
        public string Username { get; set; } = string.Empty;
        public string Color { get; set; } = string.Empty;
        public bool IsHost { get; set; } = false;
        public Position CurrentPosition { get; set; } = new Position(0, 0);
        public Position SpawnPoint { get; set; } = new Position(0, 0);
        public bool IsAlive { get; set; } = true;
        public bool IsDrawing { get; set; } = false;
        public List<Position> CurrentPath { get; set; } = new();
        public List<Territory> Territories { get; set; } = new();
        public double TotalTerritoryArea { get; set; }
        public double MaxTerritoryArea { get; set; }
        public PowerUpType? CurrentPowerUp { get; set; }
        public DateTime? PowerUpExpireTime { get; set; }
        public double MovementSpeed { get; set; } = 1.0;
        public bool HasShield { get; set; } = false;
        public bool IsGhost { get; set; } = false;
        public int KillCount { get; set; }
        public int DeathCount { get; set; }
        public long? RespawnTimestamp { get; set; }
        public int Rank { get; set; }
        
        // 出生点系统增强字段
        /// <summary>
        /// 连续死亡次数（用于死亡惩罚计算）
        /// </summary>
        public int ConsecutiveDeaths { get; set; }
        
        /// <summary>
        /// 最后死亡时间（用于连续死亡惩罚判定）
        /// </summary>
        public DateTime? LastDeathTime { get; set; }
        
        /// <summary>
        /// 无敌结束时间（复活后的无敌期）
        /// </summary>
        public DateTime? InvincibilityEndTime { get; set; }
        
        /// <summary>
        /// 当前画线长度（用于限制检查）
        /// </summary>
        public double CurrentPathLength { get; set; }
        
        /// <summary>
        /// 是否在安全区域内
        /// </summary>
        public bool IsInSafeZone { get; set; }
        
        /// <summary>
        /// 威胁警告状态（敌方接近轨迹时）
        /// </summary>
        public bool IsUnderThreat { get; set; }
        
        /// <summary>
        /// 威胁来源玩家ID
        /// </summary>
        public Guid? ThreatSourcePlayerId { get; set; }
    }

    /// <summary>
    /// 画线圈地游戏状态
    /// </summary>
    public class LineDrawingGameState
    {
        public Guid GameId { get; set; }
        public string RoomCode { get; set; } = string.Empty;
        public string GameMode { get; set; } = "classic";
        public string Status { get; set; } = "preparing";
        public DateTime CreatedAt { get; set; }
        public int MapWidth { get; set; } = 1000;
        public int MapHeight { get; set; } = 1000;
        public string MapShape { get; set; } = "circle";
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        public int Duration { get; set; } = 300; // 游戏持续时间（秒）
        public int DurationSeconds { get; set; } = 180;
        public int RemainingTimeSeconds { get; set; }
        public int MaxPlayers { get; set; } = 8;
        public bool EnablePowerUps { get; set; } = true;
        public List<LineDrawingPlayerState> Players { get; set; } = new();
        public List<PowerUpState> PowerUps { get; set; } = new();
        public SpecialEventState? CurrentSpecialEvent { get; set; }
        public bool IsInShrinkingPhase { get; set; }
        public DynamicBalanceState? DynamicBalance { get; set; }
    }

    /// <summary>
    /// 道具状态
    /// </summary>
    public class PowerUpState
    {
        public Guid Id { get; set; }
        public PowerUpType Type { get; set; }
        public Position Position { get; set; } = new Position(0, 0);
        public DateTime CreatedAt { get; set; }
        public bool IsCollected { get; set; }
    }

    /// <summary>
    /// 特殊事件状态
    /// </summary>
    public class SpecialEventState
    {
        public string Type { get; set; } = string.Empty;
        public string Name { get; set; } = string.Empty;
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public Dictionary<string, object>? Parameters { get; set; }
    }

    /// <summary>
    /// 动态平衡状态
    /// </summary>
    public class DynamicBalanceState
    {
        public bool IsActive { get; set; }
        public Guid? LeadingPlayerId { get; set; }
        public double LeadingPlayerAreaPercentage { get; set; }
        public Dictionary<Guid, BalanceAdjustment> PlayerAdjustments { get; set; } = new();
    }

    /// <summary>
    /// 平衡调整
    /// </summary>
    public class BalanceAdjustment
    {
        public string Type { get; set; } = string.Empty;
        public double Value { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 玩家排名DTO
    /// </summary>
    public class PlayerRankingDto
    {
        public Guid PlayerId { get; set; }
        public string Username { get; set; } = string.Empty;
        public string Color { get; set; } = string.Empty;
        public int Rank { get; set; }
        public double TerritoryArea { get; set; }
        public double AreaPercentage { get; set; }
        public bool IsAlive { get; set; }
        public int KillCount { get; set; }
        public int DeathCount { get; set; }
        public double KDRatio { get; set; }
    }

    /// <summary>
    /// 游戏设置
    /// </summary>
    public class GameSettings
    {
        public int Duration { get; set; } = 300; // 游戏时长（秒）
        public int MapWidth { get; set; } = 1000;
        public int MapHeight { get; set; } = 1000;
        public int MaxPlayers { get; set; } = 8;
        public bool EnablePowerUps { get; set; } = true;
        public bool EnableSpecialEvents { get; set; } = true;
        public bool EnableDynamicBalance { get; set; } = true;
        public string GameMode { get; set; } = "classic";
    }

    /// <summary>
    /// 游戏创建结果
    /// </summary>
    public class GameCreationResult
    {
        public bool Success { get; set; }
        public Guid GameId { get; set; }
        public string RoomCode { get; set; } = string.Empty;
        public LineDrawingGameState? GameState { get; set; }
        public Guid HostPlayerId { get; set; }
        public List<string> Errors { get; set; } = new();
    }

    /// <summary>
    /// 游戏加入结果
    /// </summary>
    public class GameJoinResult
    {
        public bool Success { get; set; }
        public Guid GameId { get; set; }
        public LineDrawingGameState? GameState { get; set; }
        public LineDrawingPlayerState? PlayerState { get; set; }
        public bool IsRejoining { get; set; }
        public List<string> Errors { get; set; } = new();
    }

    /// <summary>
    /// 游戏开始结果
    /// </summary>
    public class GameStartResult
    {
        public bool Success { get; set; }
        public Guid GameId { get; set; }
        public LineDrawingGameState? GameState { get; set; }
        public DateTime StartTime { get; set; }
        public int Duration { get; set; }
        public List<string> Errors { get; set; } = new();
    }

    /// <summary>
    /// 游戏离开结果
    /// </summary>
    public class GameLeaveResult
    {
        public bool Success { get; set; }
        public int RemainingPlayerCount { get; set; }
        public List<string> Errors { get; set; } = new();
    }

    /// <summary>
    /// 移动验证结果
    /// </summary>
    public class MoveValidationResult
    {
        public bool IsValid { get; set; }
        public string? ErrorMessage { get; set; }
        public double MovementSpeed { get; set; } = 1.0;
    }

    /// <summary>
    /// 碰撞结果
    /// </summary>
    public class CollisionResult
    {
        public bool HasCollision { get; set; }
        public Guid? CollidedWithPlayerId { get; set; }
        public Position? CollisionPoint { get; set; }
        public string? CollisionType { get; set; } // "player_trail", "self_trail", "boundary"
    }

    /// <summary>
    /// 领地状态信息
    /// </summary>
    public class TerritoryStatusInfo
    {
        public bool IsInOwnTerritory { get; set; }
        public bool IsInEnemyTerritory { get; set; }
        public bool IsInNeutralZone { get; set; }
        public bool IsInSafeZone { get; set; }
        public Guid? EnemyTerritoryOwner { get; set; }
    }
}